namespace DotNetNuke.Common
{
    using System;
    using System.Collections;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;

    public class XmlValidatorBase
    {
        private ArrayList _errs = new ArrayList();
        private XmlTextReader _reader;
        private XmlSchemaSet _schemaSet = new XmlSchemaSet();

        public bool IsValid()
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas = this._schemaSet;
            settings.ValidationType = ValidationType.Schema;
            settings.ValidationEventHandler += new ValidationEventHandler(this.ValidationCallBack);
            XmlReader vreader = XmlReader.Create(this._reader, settings);
            while (vreader.Read())
            {
            }
            vreader.Close();
            return (this._errs.Count == 0);
        }

        public virtual bool Validate(Stream xmlStream)
        {
            xmlStream.Seek(0L, SeekOrigin.Begin);
            this._reader = new XmlTextReader(xmlStream);
            return this.IsValid();
        }

        public virtual bool Validate(string filename)
        {
            this._reader = new XmlTextReader(filename);
            return this.IsValid();
        }

        protected void ValidationCallBack(object sender, ValidationEventArgs args)
        {
            this._errs.Add(args.Message);
        }

        public ArrayList Errors
        {
            get
            {
                return this._errs;
            }
            set
            {
                this._errs = value;
            }
        }

        public XmlSchemaSet SchemaSet
        {
            get
            {
                return this._schemaSet;
            }
        }
    }
}

